package com.jingdianjichi.practice.server.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.base.Preconditions;
import com.jingdianjichi.practice.api.common.PageResult;
import com.jingdianjichi.practice.api.common.Result;
import com.jingdianjichi.practice.api.req.*;
import com.jingdianjichi.practice.api.vo.*;
import com.jingdianjichi.practice.server.entity.dto.PracticeSetDTO;
import com.jingdianjichi.practice.server.entity.dto.PracticeSubjectDTO;
import com.jingdianjichi.practice.server.entity.po.PracticeSetDetailPO;
import com.jingdianjichi.practice.server.mapper.PracticeSetDao;
import com.jingdianjichi.practice.server.service.PracticeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.SelectKey;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
 * ClassName:PracticeSetController
 * Package:com.jingdianjichi.practice.server.controller
 * Description:
 *
 * @Author fst
 * @Create 2025/1/17 17:10
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/practice/set")
public class PracticeSetController
{

    @Resource
    private PracticeService practiceService;



    @RequestMapping("getSpecialPracticeContent")
    public Result<List<SpecialPracticeVO>>  getSpecialPracticeContent()
    {

        try {

            if(log.isInfoEnabled())
            {
                log.info("PracticeSetController.getSpecialPracticeContent:");
            }
            List<SpecialPracticeVO> list =  practiceService.getSpecialPracticeContent();

            return Result.ok(list);
        }catch (Exception e)
        {
            log.info("PracticeSetController.getSpecialPracticeContent.error:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/addPractice")
    public Result<PracticeSetVO> addPractice(@RequestBody GetPracticeSubjectListReq req)
    {
        try {
            if(log.isInfoEnabled())
            {
                log.info("PracticeSetController.addPractice:{}",req);
            }
            Preconditions.checkNotNull(req,"req不能为空");
            Preconditions.checkNotNull(CollectionUtils.isEmpty(req.getAssembleIds()),"assembleIds不能为空");

            PracticeSubjectDTO practiceSubjectDTO = new PracticeSubjectDTO();

            practiceSubjectDTO.setAssembleIds(req.getAssembleIds());

            PracticeSetVO practiceSetVO = practiceService.addPractice(practiceSubjectDTO);
            return Result.ok(practiceSetVO);
        } catch (Exception e) {
            log.info("PracticeSetController.addPractice.error:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("getSubjects")
    public Result<PracticeSubjectListVO> getSubjects(@RequestBody GetPracticeSubjectsReq req)
    {
        try {
            if(log.isInfoEnabled())
            {
                log.info("PracticeSetController.getSubjects:{}",req);
            }
            Preconditions.checkNotNull(req,"req不能为空");
            Preconditions.checkNotNull(req.getSetId(),"setId不能为空");
            PracticeSubjectListVO practiceSubjectListVO = practiceService.getSubjects(req);
            return Result.ok(practiceSubjectListVO);
        } catch (Exception e)
            {
            log.info("PracticeSetController.getSubjects.error:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }

    }

    @PostMapping("getPracticeSubject")
    public Result<PracticeSubjectVO> getPracticeSubject(@RequestBody GetPracticeSubjectReq req)
    {
        try {
            if(log.isInfoEnabled())
            {
                log.info("PracticeSetController.getPracticeSubject:{}",req);
            }
            Preconditions.checkNotNull(req,"req不能为空");
            Preconditions.checkNotNull(req.getSubjectId(),"subjectId不能为空");
            Preconditions.checkNotNull(req.getSubjectType(),"subjectType不能为空");

            PracticeSubjectDTO dto = new PracticeSubjectDTO();
            dto.setSubjectType(req.getSubjectType());
            dto.setSubjectId(req.getSubjectId());

            PracticeSubjectVO practiceSubjectVO = practiceService.getPracticeSubject(dto);

            return Result.ok(practiceSubjectVO);
        }catch (Exception e)
        {
            log.info("PracticeSetController.getPracticeSubject.error:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/getPreSetContent")
    public Result<PageResult<PracticeSetVO>> getPreSetContent(@RequestBody GetPreSetReq req)
    {
        if(log.isInfoEnabled())
        {
            log.info("PracticeSetController.getPresetContent{}", JSON.toJSONString(req));
        }

        try {
            Preconditions.checkArgument(!Objects.isNull(req),"req不能为空");
            PracticeSetDTO dto = new PracticeSetDTO();
            dto.setOrderType(req.getOrderType());
            dto.setPageInfo(req.getPageInfo());
            dto.setSetName(req.getSetName());
            PageResult<PracticeSetVO> list = practiceService.getPresetContent(dto);

            if(log.isInfoEnabled())
            {
                log.info("PracticeSetController.getPresetContent{}", JSON.toJSONString(list));
            }
            return Result.ok(list);
        }
        catch (Exception e)
        {
            log.info("PracticeSetController.getPresetContent.error:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/getUnCompletePractice")
    public Result<PageResult<UnCompletePracticeSetVO>> getUnCompletePractice(@RequestBody GetUnCompletePracticeReq req)
    {
        try {
            Preconditions.checkArgument(!Objects.isNull(req),"req不能为空");

            PageResult<UnCompletePracticeSetVO> list = practiceService.getUnCompletePractice(req);

            if(log.isInfoEnabled())
            {
                log.info("PracticeSetController.getUnCompletePractice{}", JSON.toJSONString(list));
            }
            return Result.ok(list);
        }
        catch (Exception e)
        {
            log.info("PracticeSetController.getUnCompletePractice.error:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
    }
}
